package com.glory.hikvisionmobile.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;

import com.glory.hikvisionmobile.R;

import java.util.ArrayList;
import java.util.List;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/1/8 13:51
 * @DESC *****************************
 */
public class HikLabelSpinner extends LinearLayout {

    private boolean notnull;
    private String labelStr;
    private float labelSize;
    private int labelColor;
    private float labelContextSize;
    private int labelContextColor;
    private int labelWeight;
    private int labelContentWeight;
    private String labelContentHint;
    private int labelContentHintColor;

    private TextView label;
    private Spinner labelContext;

    public HikLabelSpinner(Context context) {
        super(context);
        init(null, 0);
    }

    public HikLabelSpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HikLabelSpinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            throw new IllegalArgumentException("must be init by xml");
        }
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.HikLabelSpinner, defStyle, 0);
        notnull = array.getBoolean(R.styleable.HikLabelSpinner_notnull, false);
        labelStr = array.getString(R.styleable.HikLabelSpinner_label);
        if (labelStr == null) {
            labelStr = "Label";
        }
        labelSize = array.getDimension(R.styleable.HikLabelSpinner_labelSize, 24);
        labelColor = array.getColor(R.styleable.HikLabelSpinner_labelColor, Color.parseColor("#000000"));
        labelContextSize = array.getDimension(R.styleable.HikLabelSpinner_labelContentSize, 24);
        labelContextColor = array.getColor(R.styleable.HikLabelSpinner_labelContentColor, Color.parseColor("#000000"));
        labelWeight = array.getInt(R.styleable.HikLabelSpinner_labelWeight, 0);
        labelContentWeight = array.getInt(R.styleable.HikLabelSpinner_labelContentWeight, 0);
        labelContentHint = array.getString(R.styleable.HikLabelSpinner_labelContentHint);
        labelContentHintColor = array.getColor(R.styleable.HikLabelSpinner_labelContentHintColor, Color.parseColor("#999999"));
        array.recycle();

        inflate();
    }

    private void inflate() {
        inflate(getContext(), R.layout.hik_view_label_spinner, this);
        label = findViewById(R.id.tv_label);
        labelContext = findViewById(R.id.spinner);

        if (notnull) {
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder(labelStr + "*");
            ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#FF0000"));
            stringBuilder.setSpan(span, stringBuilder.length() - 1, stringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            label.setText(stringBuilder);
        } else {
            label.setText(labelStr);
        }
        label.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        label.setTextColor(labelColor);
        LayoutParams params1 = (LayoutParams) label.getLayoutParams();
        if (labelWeight > 0) {
            params1.weight = labelWeight;
        }
        label.setLayoutParams(params1);

        LayoutParams params2 = (LayoutParams) labelContext.getLayoutParams();
        if (labelContentWeight > 0) {
            params2.weight = labelContentWeight;
        }
        labelContext.setLayoutParams(params2);
    }

    public Spinner getLabelContext() {
        return labelContext;
    }


    public static abstract class HikSpinnerAdapter<T> implements SpinnerAdapter {

        private List<T> tList;
        private List<DataSetObserver> observers;

        public HikSpinnerAdapter(List<T> tList) {
            this.tList = tList;
            this.observers = new ArrayList<>();
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            Log.i("HikSpinnerAdapter", "registerDataSetObserver");
            this.observers.add(observer);
        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            Log.i("HikSpinnerAdapter", "unregisterDataSetObserver");
            this.observers.remove(observer);
        }

        public void setData(List<T> data){
            this.tList = data;
            notifyData();
        }

        public void notifyData() {
            for (DataSetObserver observer: this.observers) {
                observer.onChanged();
            }
        }

        public List<T> getData() {
            return this.tList;
        }

        @Override
        public int getCount() {
            return tList == null ? 0 : tList.size();
        }

        @Override
        public T getItem(int position) {
            return tList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @LayoutRes
        public abstract int getDropDownViewId();

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = LayoutInflater.from(parent.getContext()).inflate(getDropDownViewId(), null);
            }
            bindDataDownView(position, convertView, tList.get(position));
            return convertView;
        }

        public abstract void bindDataDownView(int position, View itemView, T data);

        @LayoutRes
        public abstract int getViewId();

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = LayoutInflater.from(parent.getContext()).inflate(getViewId(), null);
            }
            bindDataView(position, convertView, tList.get(position));
            return convertView;
        }

        public abstract void bindDataView(int position, View itemView, T data);

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public boolean isEmpty() {
            return tList == null || tList.size() == 0;
        }
    }
}
